
package nativesql.entity;

import java.util.List;

import model.bean.County;
import model.bean.District;

import org.hibernate.SQLQuery;

import com.e885.hibernate.HibernateSessionFactory;
import com.e885.log.E885Logger;

/**
 * return multiple Entity
 * 
 * @author Eric.Kao(gau168.gau@msa.hinet.net)
 * @version 1.0
 * @company E885 Group
 * @copyright Copyright (c) 2001-2008
 */

public class MultipleEntityNativeSQLCorrect
{
	/**
	 * 查詢台北縣的鄉鎮市
	 */
	private String districtId = "222";

	public static void main( String[] args )
	{
		new MultipleEntityNativeSQLCorrect();
	}

	@SuppressWarnings( "unchecked" )
	public MultipleEntityNativeSQLCorrect()
	{
		String sql =
					 "select {district.*}, {county.*}  from DISTRICT district, COUNTY county where district.COUNTY_ID=county.ID and district.ID=:districtId";

		SQLQuery query = HibernateSessionFactory.getSession().createSQLQuery( sql );
		query.addEntity( "county", County.class ); // 這個順序和 Object Array 有關( query.list 傳回值 )
		query.addEntity( "district", District.class ); // 這個順序和 Object Array 有關( query.list 傳回值 )

		query.setParameter( "districtId", districtId );

		List< Object[] > list = query.list();

		for( int i = 0; i < list.size(); i++ )
		{
			Object[] objs = list.get( i );

			E885Logger.testLog.debug( "=======================================================" );
			E885Logger.testLog.debug( "county:" + ( County )objs[ 0 ] );
			E885Logger.testLog.debug( "county name:" + ( ( County )objs[ 0 ] ).getName() );
			E885Logger.testLog.debug( "district:" + ( District )objs[ 1 ] );
			E885Logger.testLog.debug( "district name:" + ( ( District )objs[ 1 ] ).getName() );
		}
	}

}
